<?php

//Initiate Authenticate
SecurityRedirect ();

//initiate Vars 
define("PRIMARY_KEY","id");
$KEY[]          = array("PRIMARY_KEY"=>PRIMARY_KEY);
$FileName       = "ais_tools_tariff_packet.php";
$FileTemplate   = "ais_tools_tariff_packet_list.html";
$SubHeaderTitle = "Tariff Packet";
$PageSize       = 10 ;
$HTMLGridList   = "tariff_packet";
$TableName 	= "$DBApp.tbltariffheader";
$TableDetail= "$DBApp.tbltariffdetail";
$TableDepo 	= "$DBApp.tbldepo";


//************ FORM ACTION **************
if (GetParam("Action","")=="Delete") {
	DeleteRecord ("txtCheck",$PageSize);
}

//Initiate Database Table For Search;
$FDepoSearch   = str_replace ( "*", "%", GetParam("DepoSearch",""));

//******* DATABASE FORM QUERY ***********
$SQL = "SELECT tr.id as id,dp.name as dpname,tr.tbldepo_id as tbldepo_id,tr.date_start as date_start,tr.date_end as date_end,tr.created as created
		from $TableName tr left join $TableDepo dp on tr.tbldepo_id = dp.id ";
	
if ($FDepoSearch)
	if (strlen($WHERE)) $WHERE .= " AND "." dp.name like '%".$FDepoSearch."%'";
	else $WHERE .= " dp.name like '%".$FDepoSearch."%'";
if (strlen($WHERE))	$WHERE = " WHERE ".$WHERE;
else $WHERE = " WHERE 1";

if(strlen($WHERE)>0)
	$SQL = $SQL.$WHERE." GROUP BY tr.tbldepo_id ORDER BY tr.id DESC";
else
	$SQL = $SQL." GROUP BY tr.tbldepo_id ORDER BY tr.id DESC";
//******* SHOW FORM CONTENT *********
//echo $SQL;
$FDepoSearch   = str_replace ( "%", "*", GetParam("DepoSearch",""));

$PageNum = 1;
if ( empty($RecCnt)) $RecCnt=0;

//Default value
if (!isset($_GET)) $_GET=&$HTTP_GET_VARS ;
if (isset($_GET['PageNum'])) {
  $PageNum = $_GET['PageNum'] ;
} else {
    $PageNum = 1 ;
}

//Default value
if (isset($_GET['RecCnt'])) {
  $RecCnt = intval($_GET['RecCnt']) ;
} else {
    $RecCnt = -1 ;
} 
$TBS = new clsTinyButStrong ;
$TBS->LoadTemplate($MOD_TEMPLATE_DIR."/".$FileTemplate);
$TBS->MergeBlock("bln",$KEY);
$RecCnt = $TBS->MergeBlock("blg",$DBMysql,$SQL,$PageSize,$PageNum,$RecCnt);
$TBS->MergeNavigationBar('nv',array('size'=>10,'pos'=>'centred'),$PageNum,$RecCnt,$PageSize) ; 

$TBS->Show();


function DeleteRecord ($sequence_name,$PageSize) {
	global $DBCoins,$DBName,$DBConnection,$FileName,$TableName;
	
    if (GetParam("PageNum","")) {
          $StartRecord = GetParam("PageNum","")*$PageSize-$PageSize+1;
          $EndRecord   = $StartRecord+$PageSize-1;
    } 
    //print 'asdsad'; exit;
	for ($x=$StartRecord;$x<=$EndRecord;$x++) {
		if (GetParam($sequence_name.$x,""))	{
			$DBConnection->Delete("$TableName",$sequence_name.$x);
			
			// fk
			$DBConnection->dbc->query("DELETE from agent_agreement_charge where agent_agr_oid='".GetParam($sequence_name.$x,"")."'");
			//$DBConnection->dbc->query("DELETE from depo_agreement_labor where depo_agr_oid='".GetParam($sequence_name.$x,"")."'");
	    }
	}
	header("Location: $FileName");
	exit;	
	
}

?>
